﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using SystemDiagramNew.SystemDiagramLegend;
using SystemDiagramNew.SystemDiagramLegend.DataDefine;
using SystemDiagramNew.TypeDefinition;
using SystemDiagramNew.Utils;

namespace SystemDiagramNew
{
	public class DrawingKit
	{
		public static void Print(string sData)
		{
			try
			{
				string path = "C:\\temp.txt";
				if (!File.Exists(path))
				{
					FileStream fileStream = File.Create(path);
					fileStream.Close();
				}
				string str = DateTime.Now.ToString("HH:mm:ss");
				StreamWriter streamWriter = File.AppendText(path);
				streamWriter.WriteLine(str + sData);
				streamWriter.Close();
			}
			catch (Exception)
			{
			}
		}

		public DrawingKit(ExternalCommandData cmdData, double shaftSideAngle, double legendScale)
		{
			Node.Revit = cmdData;
			PipeInfo.Revit = cmdData;
			this.m_Revit = cmdData;
			this.m_ShaftSideAngle = Geometry.formatAngle(shaftSideAngle / 180.0 * Math.PI);
			this.m_LegendScale = legendScale;
			this.m_LegendManager = new DrawingKit.LegendInfoFileManager();
			this.m_TextNoteTypeWithLabel = this.m_Revit.CreateTextNoteType();
			this.m_ViewScale = this.m_Revit.Application.ActiveUIDocument.ActiveView.Scale;
			this.IsVertical = false;
		}

		public Result Drawing(PipingSystemInfo pipingSystemInfo, bool labelPipeDiameter, bool labelElevation)
		{
			this.SetProceeding("正在绘制轴测图...", 1);
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<Line> legendLineList = new List<Line>();
			this.SetProceeding("正在绘制图例...", 1);
			Result result = this.DrawingNode(pipingSystemInfo, ref legendLineList);
			if (result != null)
			{
				return result;
			}
			this.SetProceeding("", 1);
			this.SetPipeLabelInfo(ref pipingSystemInfo);
			this.SetProceeding("", 1);
			this.SetLevelLabelInfo(ref pipingSystemInfo);
			this.SetProceeding("正在绘制管线...", 1);
			result = this.DrawingPipe(pipingSystemInfo, legendLineList, labelPipeDiameter, labelElevation);
			if (result != null)
			{
				return result;
			}
			this.SetProceeding("马上就要完成了，请稍等...", 1);
			result = this.DrawingSystemName(pipingSystemInfo);
			if (result != null)
			{
				return result;
			}
			this.SetProceeding("", 1);
			if (this.m_CurveAry.IsEmpty)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			View view = this.View;
			Result result2;
			try
			{
				DetailCurveArray detailCurveArray = document.Create.NewDetailCurveArray(view, this.m_CurveAry);
				result = this.AddExtensibleStorage(detailCurveArray);
				if (PipeInfo.BLineReplace)
				{
					LineTypeOperate.SetLineStyleId(this.m_Revit.Application.ActiveUIDocument.Document, ref detailCurveArray);
				}
				result2 = result;
			}
			catch (Exception ex)
			{
				Log.Error(new string[]
				{
					ex.Message
				});
				result2 = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result2;
		}

		public void DrawingLegend(Legend legend, XYZ ptInsert)
		{
			List<Line> list = new List<Line>();
			Transform transform = Transform.Identity;
			transform = transform.ScaleBasis(this.m_LegendScale);
			this.DrawNodeLegend(legend, transform, ptInsert, ref list);
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			View view = this.View;
			new DetailCurveArray();
			try
			{
				document.Create.NewDetailCurveArray(view, this.m_CurveAry);
			}
			catch (Exception)
			{
			}
		}

		public static void DrawingPipeDiameter(Element elem, int diameter, XYZ ptStart, XYZ ptEnd, XYZ ptInsert, double legendScale, ExternalCommandData cmdData, View drawView)
		{
			TextNoteType textNodeType = cmdData.CreateTextNoteType();
			DrawingKit.DrawingPipeLabel(elem, diameter, ptStart, ptEnd, ptInsert, textNodeType, cmdData, drawView, false);
		}

		public static void DrawingPipeElevation(double startElevation, double endElevation, XYZ ptStart, XYZ ptEnd, XYZ ptSelect, double legendScale, DrawingKit.LabelType labelType, ExternalCommandData cmdData, View drawView)
		{
			TextNoteType textNoteType = cmdData.CreateTextNoteType();
			int scale = cmdData.Application.ActiveUIDocument.ActiveView.Scale;
			CurveArray curveArray = new CurveArray();
			if (labelType == DrawingKit.LabelType.LT_SelectPoint)
			{
				double elevation = startElevation;
				if (!Geometry.IsEqual(startElevation, endElevation))
				{
					double length = (ptEnd - ptStart).GetLength();
					double length2 = (ptSelect - ptStart).GetLength();
					elevation = startElevation + (endElevation - startElevation) * length2 / length;
				}
				XYZ direction = (ptEnd - ptStart).Normalize();
				curveArray = DrawingKit.DrawElevationLabel(ptSelect, direction, elevation, legendScale, textNoteType, scale, cmdData, drawView, false, false);
			}
			else
			{
				XYZ ptInsert = ptEnd;
				double elevation2 = endElevation;
				XYZ xyz = (ptEnd - ptStart).Normalize();
				double length3 = (ptEnd - ptStart).GetLength();
				double length4 = (ptSelect - ptStart).GetLength();
				if (Geometry.LessThan(length4, length3 / 2.0))
				{
					ptInsert = ptStart;
					elevation2 = startElevation;
					xyz = -xyz;
				}
				bool offset = true;
				bool drawBottomLine = true;
				if (labelType == DrawingKit.LabelType.LT_NearInside)
				{
					offset = false;
					drawBottomLine = false;
					xyz = -xyz;
				}
				curveArray = DrawingKit.DrawElevationLabel(ptInsert, xyz, elevation2, legendScale, textNoteType, scale, cmdData, drawView, offset, drawBottomLine);
			}
			new DetailCurveArray();
			try
			{
				cmdData.Application.ActiveUIDocument.Document.Create.NewDetailCurveArray(drawView, curveArray);
				cmdData.Application.ActiveUIDocument.UpdateModel(false);
			}
			catch (Exception)
			{
			}
		}

		private void SetPipeLabelInfo(ref PipingSystemInfo pipingSystemInfo)
		{
			List<PipeInfo> list = pipingSystemInfo.PipeInfoList();
			Dictionary<int, List<PipeInfo>> dictionary = new Dictionary<int, List<PipeInfo>>();
			foreach (PipeInfo pipeInfo in list)
			{
				int diameter = pipeInfo.Diameter;
				if (!dictionary.ContainsKey(diameter))
				{
					dictionary.Add(diameter, new List<PipeInfo>
					{
						pipeInfo
					});
				}
				else
				{
					dictionary[diameter].Add(pipeInfo);
				}
			}
			foreach (KeyValuePair<int, List<PipeInfo>> keyValuePair in dictionary)
			{
				List<PipeInfo> value = keyValuePair.Value;
				List<List<PipeInfo>> linkedPipeInfoSet = this.GetLinkedPipeInfoSet(value);
				foreach (List<PipeInfo> set in linkedPipeInfoSet)
				{
					this.SetPipeLabelInfoBySet(set, pipingSystemInfo);
				}
			}
		}

		private void SetPipeLabelInfoBySet(List<PipeInfo> set, PipingSystemInfo pipingSystemInfo)
		{
			if (set.Count != 1)
			{
				PipeInfo pipeInfo = null;
				double val = 0.0;
				foreach (PipeInfo pipeInfo2 in set)
				{
					XYZ startPoint = pipeInfo2.StartPoint;
					XYZ endPoint = pipeInfo2.EndPoint;
					if (pipeInfo2.IsStandpipe())
					{
						Node node = null;
						if (pipingSystemInfo.FindNodeByPosition(startPoint, ref node) && (node.TheNodeType == Node.NodeType.NT_SPRINKLERS || node.TheNodeType == Node.NodeType.NT_PLUMBING_FIXTURES || node.TheNodeType == Node.NodeType.NT_MECHANICAL_EQUIPMENT))
						{
							continue;
						}
						node = null;
						if (pipingSystemInfo.FindNodeByPosition(endPoint, ref node) && (node.TheNodeType == Node.NodeType.NT_SPRINKLERS || node.TheNodeType == Node.NodeType.NT_PLUMBING_FIXTURES || node.TheNodeType == Node.NodeType.NT_MECHANICAL_EQUIPMENT))
						{
							continue;
						}
					}
					double num = startPoint.DistanceTo(endPoint);
					if (Geometry.LessThan(val, num))
					{
						pipeInfo = pipeInfo2;
						val = num;
					}
				}
				if (pipeInfo != null)
				{
					pipeInfo.DrawingPipeLabel = true;
				}
				return;
			}
			if (set[0].IsStandpipe())
			{
				return;
			}
			set[0].DrawingPipeLabel = true;
		}

		private void SetLevelLabelInfo(ref PipingSystemInfo pipingSystemInfo)
		{
			List<PipeInfo> list = pipingSystemInfo.PipeInfoList();
			Dictionary<int, List<PipeInfo>> dictionary = new Dictionary<int, List<PipeInfo>>();
			foreach (PipeInfo pipeInfo in list)
			{
				if (Geometry.IsEqual(pipeInfo.StartElevation, pipeInfo.EndElevation))
				{
					int @int = this.GetInt(pipeInfo.StartElevation * 1000.0);
					if (!dictionary.ContainsKey(@int))
					{
						dictionary.Add(@int, new List<PipeInfo>
						{
							pipeInfo
						});
					}
					else
					{
						dictionary[@int].Add(pipeInfo);
					}
				}
			}
			foreach (KeyValuePair<int, List<PipeInfo>> keyValuePair in dictionary)
			{
				List<PipeInfo> value = keyValuePair.Value;
				List<List<PipeInfo>> linkedPipeInfoSet = this.GetLinkedPipeInfoSet(value);
				foreach (List<PipeInfo> set in linkedPipeInfoSet)
				{
					this.SetLevelLabelInfoBySet(set, pipingSystemInfo);
				}
			}
		}

		private int GetInt(double a)
		{
			if (a == 0.0)
			{
				return 0;
			}
			double num;
			if (a > 0.0)
			{
				num = a + 0.99999999999999989;
			}
			else
			{
				num = a + -0.999999999999999;
			}
			return (int)num;
		}

		private void SetLevelLabelInfoBySet(List<PipeInfo> set, PipingSystemInfo pipingSystemInfo)
		{
			if (set.Count == 1)
			{
				PipeInfo pipeInfo = set[0];
				this.SettedLevelLabel(pipeInfo, pipeInfo.StartPoint, pipingSystemInfo);
				return;
			}
			Dictionary<XYZ, List<PipeInfo>> dictionary = new Dictionary<XYZ, List<PipeInfo>>();
			foreach (PipeInfo pipeInfo2 in set)
			{
				XYZ startPoint = pipeInfo2.StartPoint;
				XYZ endPoint = pipeInfo2.EndPoint;
				if (!dictionary.ContainsKey(startPoint))
				{
					dictionary.Add(startPoint, new List<PipeInfo>
					{
						pipeInfo2
					});
				}
				else
				{
					List<PipeInfo> list = dictionary[startPoint];
					list.Add(pipeInfo2);
				}
				if (!dictionary.ContainsKey(endPoint))
				{
					dictionary.Add(endPoint, new List<PipeInfo>
					{
						pipeInfo2
					});
				}
				else
				{
					List<PipeInfo> list2 = dictionary[endPoint];
					list2.Add(pipeInfo2);
				}
			}
			foreach (KeyValuePair<XYZ, List<PipeInfo>> keyValuePair in dictionary)
			{
				XYZ key = keyValuePair.Key;
				List<PipeInfo> value = keyValuePair.Value;
				if (value.Count == 1 && this.SettedLevelLabel(value[0], key, pipingSystemInfo))
				{
					break;
				}
			}
		}

		private bool SettedLevelLabel(PipeInfo pipeInfo, XYZ pos, PipingSystemInfo pipingSystemInfo)
		{
			Node node = null;
			if (pipingSystemInfo.FindNodeByPosition(pos, ref node))
			{
				if (pipeInfo.StartPoint.IsAlmostEqualTo(pos))
				{
					pipeInfo.LevelLabelPosition = LevelLabelPosition.Start;
				}
				else
				{
					pipeInfo.LevelLabelPosition = LevelLabelPosition.End;
				}
				return true;
			}
			return false;
		}

		private List<List<PipeInfo>> GetLinkedPipeInfoSet(List<PipeInfo> pipeInfoList)
		{
			List<List<PipeInfo>> list = new List<List<PipeInfo>>();
			while (pipeInfoList.Count > 0)
			{
				List<PipeInfo> list2 = new List<PipeInfo>();
				PipeInfo pipeInfo = pipeInfoList[0];
				list2.Add(pipeInfo);
				pipeInfoList.RemoveAt(0);
				this.FindLindedPipeInfo(pipeInfo, ref pipeInfoList, ref list2);
				list.Add(list2);
			}
			return list;
		}

		private void FindLindedPipeInfo(PipeInfo pipeInfo, ref List<PipeInfo> pipeInfoList, ref List<PipeInfo> linkedPipeInfoList)
		{
			for (int i = 0; i < pipeInfoList.Count; i++)
			{
				PipeInfo pipeInfo2 = pipeInfoList[i];
				if (pipeInfo.IsLinked(pipeInfo2))
				{
					linkedPipeInfoList.Add(pipeInfo2);
					pipeInfoList.RemoveAt(i);
					this.FindLindedPipeInfo(pipeInfo2, ref pipeInfoList, ref linkedPipeInfoList);
					i--;
				}
			}
		}

		private Result DrawingPipe(PipingSystemInfo pipingSystemInfo, List<Line> legendLineList, bool labelPipeDiameter, bool labelElevation)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			IEnumerable<PipeInfo> enumerable = from pi in pipingSystemInfo.PipeInfoList()
			where pi.Component != null && ElementId.InvalidElementId != pi.Component.Id
			select pi;
			foreach (PipeInfo pipeInfo in enumerable)
			{
				this.SetProceeding("", 1);
				pipeInfo.DrawSeparationLine(this.View);
				this.DrawingAnnotation(pipingSystemInfo, pipeInfo, labelPipeDiameter, labelElevation);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DrawingAnnotation(PipingSystemInfo pipingSystemInfo, PipeInfo pipeInfo, bool labelPipeDiameter, bool labelElevation)
		{
			if (labelPipeDiameter)
			{
				this.DrawingPipeLabel(pipeInfo.Pipe, pipeInfo.StartPoint, pipeInfo.EndPoint, pipeInfo.Diameter);
			}
			if (!false)
			{
				return;
			}
			if (pipeInfo.LevelLabelPosition == LevelLabelPosition.Null)
			{
				return;
			}
			XYZ pos = (pipeInfo.LevelLabelPosition == LevelLabelPosition.Start) ? pipeInfo.StartPoint : pipeInfo.EndPoint;
			Node node = null;
			if (pipingSystemInfo.FindNodeByPosition(pos, ref node))
			{
				this.DrawElevationByPipe(pipeInfo, this.GetXY(node.Position), node.TheNodeType);
			}
		}

		private void CalculateSplitPointByLengendLines(ref PipeInfo pipeInfo, ref List<Line> legendLineList)
		{
			this.SetProceeding("", 1);
			XYZ xy = this.GetXY(pipeInfo.StartPoint);
			XYZ xy2 = this.GetXY(pipeInfo.EndPoint);
			Line line = DrawingKit.CreateLine(xy, xy2);
			if (null == line)
			{
				return;
			}
			foreach (Line line2 in legendLineList)
			{
				IntersectionResultArray intersectionResultArray;
				SetComparisonResult setComparisonResult = line.Intersect(line2, out intersectionResultArray);
				if (setComparisonResult == SetComparisonResult.Overlap)
				{
					IntersectionResult intersectionResult = intersectionResultArray.get_Item(0);
					XYZ xyzpoint = intersectionResult.XYZPoint;
					if (!xyzpoint.IsAlmostEqualTo(xy) && !xyzpoint.IsAlmostEqualTo(xy2) && !xyzpoint.IsAlmostEqualTo(line2.GetEndPoint(0)) && !xyzpoint.IsAlmostEqualTo(line2.GetEndPoint(1)))
					{
						pipeInfo.AddSplitePoint(xyzpoint);
					}
				}
			}
		}

		private Result DrawingSystemName(PipingSystemInfo pipingSystemInfo)
		{
			this.SetProceeding("", 1);
			List<Node> nodeList = pipingSystemInfo.NodeList;
			XYZ position = nodeList[0].Position;
			double x = position.X;
			double x2 = position.X;
			double y = position.Y;
			double y2 = position.Y;
			for (int i = 1; i < nodeList.Count; i++)
			{
				XYZ position2 = nodeList[i].Position;
				if (Geometry.LessThan(position2.X, x))
				{
					x = position2.X;
				}
				if (Geometry.GreaterThan(position2.X, x2))
				{
					x2 = position2.X;
				}
				if (Geometry.LessThan(position2.Y, y))
				{
					y = position2.Y;
				}
				if (Geometry.GreaterThan(position2.Y, y2))
				{
					y2 = position2.Y;
				}
			}
			double num = x2 - x;
			double num2 = this.m_TextNoteTypeWithLabel.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
			num2 *= 2.0;
			double num3 = num2 * this.m_TextNoteTypeWithLabel.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			XYZ xyz = new XYZ(x + num / 2.0, y - 1.2 * num2 * (double)this.View.Scale, 0.0);
			string sysName = pipingSystemInfo.SysName;
			double num4 = num3 * sysName.GetTextNoteLength();
            DrawingKit.DrawText(xyz, num4, num2, sysName, this.m_TextNoteTypeWithLabel, (TextAlignFlags)1152, this.m_Revit, this.View);
			double num5 = num4 * (double)this.View.Scale * 0.4;
			double num6 = -(num2 + UnifiedModified.GetSystemDiagramDrawSize()[0]) * (double)this.View.Scale / 2.0;
			XYZ xyz2 = xyz + XYZ.BasisY * num6 - XYZ.BasisX * num5 / 2.0;
			XYZ xyz3 = xyz + XYZ.BasisY * num6 + XYZ.BasisX * num5 / 2.0;
			this.View.NewDetailLine(xyz2, xyz3);
			num6 = (0.5 * (double)this.View.Scale).MMtoFeet();
			XYZ start = xyz2 - XYZ.BasisY * num6;
			XYZ end = xyz3 - XYZ.BasisY * num6;
			this.View.NewDetailLine(start, end);
			this.View.CropBox = new BoundingBoxXYZ
			{
				Min = new XYZ(x, xyz.Y, 0.0),
				Max = new XYZ(x2, y2, 0.0)
			};
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Result AddExtensibleStorage(DetailCurveArray detailCurveArray)
		{
			foreach (KeyValuePair<int, Pipe> keyValuePair in this.m_PipeLineIndexInCurveAry)
			{
				DetailCurve curve = detailCurveArray.get_Item(keyValuePair.Key);
				ExtensibleStorageManager.WritePipeExtensibleStorageToDetailCurve(keyValuePair.Value, curve, this.m_LegendScale);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DrawingPipeLabel(Pipe pipe, XYZ ptStart, XYZ ptEnd, int diameter)
		{
			XYZ mid = ptStart + (ptEnd - ptStart) / 2.0;
			DrawingKit.DrawingPipeLabel(pipe, diameter, ptStart, ptEnd, mid, this.m_TextNoteTypeWithLabel, this.m_Revit, this.View, true);
		}

		private static string AngleToString(double ang)
		{
			return Convert.ToInt32(ang * 180.0 / Math.PI).ToString();
		}

		private static void DrawingPipeLabel(Element elem, int nDiameter, XYZ start, XYZ end, XYZ mid, TextNoteType textNodeType, ExternalCommandData cmdData, View drawView, bool coverFilter)
		{
			Log.Enabled = false;
			XYZ xy = start.GetXY();
			XYZ xy2 = end.GetXY();
			XYZ xy3 = mid.GetXY();
			if (xy.IsAlmostEqualTo(xy2))
			{
				return;
			}
			double val = xy.DistanceTo(xy2);
			XYZ xyz = (xy2 - xy).Normalize();
			if (Geometry.LessThan(xy2.X, xy.X))
			{
				xyz = -xyz;
			}
			else if (Geometry.IsEqual(xy2.X, xy.X) && Geometry.LessThan(xy2.Y, xy.Y))
			{
				xyz = -xyz;
			}
			string text = elem.GetPipeDiameterPrefix() + nDiameter;
			double num = DrawingKit.TextWidth * text.GetTextNoteLength();
			int scale = cmdData.Application.ActiveUIDocument.ActiveView.Scale;
			if (coverFilter && Geometry.LessThan(val, num * (double)scale))
			{
				return;
			}
			XYZ perpendicularVector = xyz.GetPerpendicularVector();
			XYZ ptTextCenter = xy3 + perpendicularVector * (0.5 * DrawingKit.TextHeight + DrawingKit.TextOffsetLine + UnifiedModified.GetSystemDiagramDrawSize()[1]) * (double)scale;
			drawView.CreateTextNote(text, ptTextCenter, xyz);
		}

		public static string ToString(string pref, XYZ point)
		{
			return pref + string.Format(" Point  = [{0} , {1},  {2}]", point.X, point.Y, point.Z);
		}

		private Result DrawingNode(PipingSystemInfo pipingSystemInfo, ref List<Line> legendLineList)
		{
			List<Node> nodeList = pipingSystemInfo.NodeList;
			Transform transform = Transform.Identity;
			transform = transform.ScaleBasis(this.m_LegendScale);
			Dictionary<ElementId, List<Node>> dictionary = new Dictionary<ElementId, List<Node>>();
			List<ElementId> list = new List<ElementId>();
			foreach (Node node in from nd in nodeList
			where nd.PipeList.Count > 0 && nd.TheNodeType != Node.NodeType.NT_NULL && nd.TheNodeType != Node.NodeType.NT_PIPEFITTING && nd.TheNodeType != Node.NodeType.NT_PIPE_TRANSITION_PIECE
			select nd)
			{
				if (node.TheNodeType == Node.NodeType.NT_PIPEFITTING_TRAP_S || node.TheNodeType == Node.NodeType.NT_PIPEFITTING_TRAP_P)
				{
					if (!dictionary.ContainsKey(node.Component.Id))
					{
						List<Node> list2 = new List<Node>();
						list2.Add(node);
						dictionary.Add(node.Component.Id, list2);
					}
					else
					{
						dictionary[node.Component.Id].Add(node);
					}
				}
				if (!list.Contains(node.Component.Id))
				{
					node.Draw(this.View, this.m_ShaftSideAngle, this.m_LegendManager, transform);
					list.Add(node.Component.Id);
				}
			}
			foreach (Node node2 in from nd in nodeList
			where nd.PipeList.Count == 0 && nd.TheNodeType == Node.NodeType.NT_PIPEACCESSORY
			select nd)
			{
				if (!list.Contains(node2.Component.Id))
				{
					this.DrawAccessoryDisconnectPipe(node2, list, transform);
				}
			}
			foreach (KeyValuePair<ElementId, List<Node>> keyValuePair in dictionary)
			{
				if (keyValuePair.Value.Count == 2)
				{
					Node node3 = keyValuePair.Value[0];
					Node node4 = keyValuePair.Value[1];
					if (node3.TheNodeType == Node.NodeType.NT_PIPEFITTING_TRAP_S)
					{
						this.DrawSTrap(node3, node4, transform);
					}
					else if (node3.TheNodeType == Node.NodeType.NT_PIPEFITTING_TRAP_P)
					{
						this.DrawPTrap(node3, node4, transform);
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DrawAccessoryDisconnectPipe(Node accessoryNode, List<ElementId> drawnElementIds, Transform scaleMatrix)
		{
			if (accessoryNode.TheNodeType == Node.NodeType.NT_PIPEACCESSORY && accessoryNode.FamilyName == "上接水角阀" && accessoryNode.IsConnectPosition && !drawnElementIds.Contains(accessoryNode.Element.Id))
			{
				accessoryNode.Draw(this.View, this.m_ShaftSideAngle, this.m_LegendManager, scaleMatrix);
				drawnElementIds.Add(accessoryNode.Component.Id);
			}
		}

		private void DrawSTrap(Node node1, Node node2, Transform scaleMatrix)
		{
			this.SetProceeding("", 1);
			if (node1.PipeList.Count < 0 || node2.PipeList.Count < 0)
			{
				this.AddLine(node1.Position, node2.Position, scaleMatrix);
				return;
			}
			XYZ xy = node1.Position.GetXY();
			XYZ xy2 = node2.Position.GetXY();
			PipeInfo pipeInfo = node1.PipeList[0];
			XYZ xyz = pipeInfo.CurStartPoint(node1.Position);
			XYZ xyz2 = pipeInfo.CurEndPoint(node1.Position);
			XYZ xyz3 = (new XYZ(0.0, xyz2.Y, 0.0) - new XYZ(0.0, xyz.Y, 0.0)).Normalize();
			double num = (xy2.X - xy.X) / 2.0;
			XYZ xyz4 = xy + XYZ.BasisX * num;
			XYZ xyz5 = xy2 - XYZ.BasisX * num;
			this.AddLine(xyz4, xyz5, scaleMatrix);
			XYZ xyz6 = (new XYZ(0.0, xyz5.Y, 0.0) - new XYZ(0.0, xyz4.Y, 0.0)).Normalize();
			XYZ xyz7 = xy;
			XYZ ptStart = xy2;
			if (!xyz3.IsAlmostEqualTo(xyz6))
			{
				XYZ xyz8 = xyz5;
				xyz5 = xyz4;
				xyz4 = xyz8;
				xyz7 = xyz4 - XYZ.BasisX * num;
				this.AddLine(xyz7, xy, scaleMatrix);
				ptStart = xyz5 + XYZ.BasisX * num;
				this.AddLine(ptStart, xy2, scaleMatrix);
			}
			XYZ xyz9 = Geometry.CalculatMidPoint(xyz7, xyz4);
			xyz9 -= xyz3 * Math.Abs(num / 2.0);
			this.AddArc(xyz4, xyz7, xyz9, scaleMatrix);
			XYZ xyz10 = Geometry.CalculatMidPoint(ptStart, xyz5);
			xyz10 += xyz3 * Math.Abs(num / 2.0);
			this.AddArc(ptStart, xyz5, xyz10, scaleMatrix);
		}

		private void DrawPTrap(Node node1, Node node2, Transform scaleMatrix)
		{
			this.SetProceeding("", 1);
			if (node1.PipeList.Count < 0 || node2.PipeList.Count < 0)
			{
				this.AddLine(node1.Position, node2.Position, scaleMatrix);
				return;
			}
			PipeInfo pipeInfo = node1.PipeList[0];
			XYZ pt = pipeInfo.CurStartPoint(node1.Position);
			XYZ pt2 = pipeInfo.CurEndPoint(node1.Position);
			XYZ ptOut = (pt2.GetXY() - pt.GetXY()).Normalize();
			PipeInfo pipeInfo2 = node2.PipeList[0];
			XYZ pt3 = pipeInfo2.CurStartPoint(node2.Position);
			XYZ pt4 = pipeInfo2.CurEndPoint(node2.Position);
			XYZ ptOut2 = (this.GetXY(pt4) - this.GetXY(pt3)).Normalize();
			double angle = Geometry.getAngle(XYZ.Zero, ptOut);
			double val = Geometry.IntersectionAngle(Math.PI*.5, angle);
			double angle2 = Geometry.getAngle(XYZ.Zero, ptOut2);
			double val2 = Geometry.IntersectionAngle(Math.PI*.5, angle2);
			XYZ xyz = this.GetXY(node1.Position);
			XYZ xy = this.GetXY(node2.Position);
			if (!Geometry.LessThan(val, val2))
			{
				xyz = this.GetXY(node2.Position);
				xy = this.GetXY(node1.Position);
			}
			double num = (xy.X - xyz.X) / 3.0;
			if (Geometry.LessThan(xy.Y, xyz.Y))
			{
				XYZ xyz2 = xy - XYZ.BasisX * num * 3.0;
				this.AddLine(xyz2, xyz, scaleMatrix);
				xyz = xyz2;
			}
			XYZ xyz3 = xyz + XYZ.BasisX * num;
			this.AddLine(xy, xyz3, scaleMatrix);
			XYZ xyz4 = Geometry.CalculatMidPoint(xyz3, xyz);
			xyz4 -= XYZ.BasisY * Math.Abs(num / 2.0);
			this.AddArc(xyz3, xyz, xyz4, scaleMatrix);
		}

		private void DrawNodeLegendByGroup(Node node, LegendGroup legendGroup, Transform scaleMatrix, ref List<Line> legendLineList)
		{
			LegendDirection legendDirection = node.GetLegendDirection(this.m_ShaftSideAngle);
			this.DrawNodeLegendByGroup(legendDirection, node, legendGroup, scaleMatrix, ref legendLineList);
		}

		private void DrawNodeLegendByGroup(LegendDirection legendDir, Node node, LegendGroup legendGroup, Transform scaleMatrix, ref List<Line> legendLineList)
		{
			this.SetProceeding("", 1);
			Legend legendByPipeDirection = this.GetLegendByPipeDirection(legendDir, legendGroup);
			this.SetProceeding("", 1);
			this.DrawNodeLegend(legendByPipeDirection, scaleMatrix, node, ref legendLineList);
		}

		private void AddLine(XYZ ptStart, XYZ ptEnd, Transform scaleMatrix)
		{
			Line line = this.DrawLineWidthLegend(this.GetXY(ptStart), this.GetXY(ptEnd), scaleMatrix);
			if (null != line)
			{
				this.m_CurveAry.Append(line);
			}
		}

		private void AddArc(XYZ ptStart, XYZ ptEnd, XYZ ptMiddle, Transform scaleMatrix)
		{
			XYZ ptStart2 = Geometry.TransformPoint(this.GetXY(ptStart), scaleMatrix);
			XYZ ptEnd2 = Geometry.TransformPoint(this.GetXY(ptEnd), scaleMatrix);
			XYZ ptMid = Geometry.TransformPoint(this.GetXY(ptMiddle), scaleMatrix);
			Arc arc = null;
			try
			{
				arc = DrawingKit.CreateArc(this.m_Revit, ptStart2, ptEnd2, ptMid);
			}
			catch (Exception)
			{
				return;
			}
			if (null != arc)
			{
				this.m_CurveAry.Append(arc);
			}
		}

		private FilledRegion DrawFilledRegion(FillRegion fillRegion, ElementId filledRegionTypeId, Transform transform)
		{
			this.SetProceeding("", 1);
			CurveLoop curveLoop = new CurveLoop();
			foreach (EdgeInfo edgeInfo in fillRegion.EdgeLoop)
			{
				Curve curve;
				if (edgeInfo.IsArc)
				{
					XYZ startPoint = edgeInfo.StartPoint;
					XYZ endPoint = edgeInfo.EndPoint;
					XYZ ptCenter = null;
					Geometry.GetCenterWithBulge(startPoint, endPoint, edgeInfo.Bulge, ref ptCenter);
					XYZ basisZ = XYZ.BasisZ;
					if (Geometry.LessThan(edgeInfo.Bulge, 0.0))
					{
                        //-XYZ.BasisZ;
					}
					XYZ ptMid = Geometry.CalculatMidPoint(startPoint, endPoint, ptCenter, XYZ.BasisZ);
					curve = DrawingKit.CreateArc(this.m_Revit, startPoint, endPoint, ptMid);
					if (null == curve)
					{
						continue;
					}
				}
				else
				{
					curve = DrawingKit.CreateLine(edgeInfo.StartPoint, edgeInfo.EndPoint);
					if (null == curve)
					{
						continue;
					}
				}
				Curve curve2 = RevitVersionFuncs.CreateTransformed(transform, curve);
				try
				{
					curveLoop.Append(curve2);
				}
				catch (Exception)
				{
					break;
				}
			}
			List<CurveLoop> list = new List<CurveLoop>();
			list.Add(curveLoop);
			try
			{
				return FilledRegion.Create(this.m_Revit.Application.ActiveUIDocument.Document, filledRegionTypeId, this.m_Revit.Application.ActiveUIDocument.ActiveView.Id, list);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
			return null;
		}

		private FillPatternElement GetSolidPattern(string patternName)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(FillPatternElement)).ToElements();
			foreach (Element element in list)
			{
				FillPatternElement fillPatternElement = element as FillPatternElement;
				if (fillPatternElement != null && fillPatternElement.Name.CompareTo(patternName) == 0)
				{
					return fillPatternElement;
				}
			}
			return null;
		}

		private ElementId GetFilledRegionTypeId()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(FilledRegionType)).ToElements();
			string text = "HY_系统图图例填充";
			foreach (Element element in list)
			{
				FilledRegionType filledRegionType = element as FilledRegionType;
				if (filledRegionType != null && filledRegionType.Name.CompareTo(text) == 0)
				{
					return filledRegionType.Id;
				}
			}
			if (list.Count < 1)
			{
				return null;
			}
			FilledRegionType filledRegionType2 = list[0] as FilledRegionType;
			FilledRegionType filledRegionType3 = filledRegionType2.Duplicate(text) as FilledRegionType;
			if (filledRegionType3 == null)
			{
				return null;
			}
			FillPatternElement solidPattern = this.GetSolidPattern("实体填充");
			if (solidPattern != null)
			{
				filledRegionType3.FillPatternId = solidPattern.Id;
			}
			try
			{
				filledRegionType3.Color = new Color(0, 0, 0);
			}
			catch (Exception)
			{
				return filledRegionType3.Id;
			}
			return filledRegionType3.Id;
		}

		private Legend GetLegendByPipeDirection(Node node, LegendGroup legendGroup)
		{
			this.SetProceeding("", 1);
			if (this.IsVertical)
			{
				return legendGroup.LeftLink;
			}
			LegendDirection legendDirection = node.GetLegendDirection(this.m_ShaftSideAngle);
			return legendGroup.GetLegend(legendDirection);
		}

		private Legend GetLegendByPipeDirection(LegendDirection legendDirection, LegendGroup legendGroup)
		{
			this.SetProceeding("", 1);
			if (this.IsVertical)
			{
				return legendGroup.LeftLink;
			}
			switch (legendDirection)
			{
			case LegendDirection.LeftLink:
				return legendGroup.LeftLink;
			case LegendDirection.TopLink:
				return legendGroup.TopLink;
			case LegendDirection.RightLink:
				return legendGroup.RightLink;
			default:
				return legendGroup.BottomLink;
			}
		}

		private void DrawElevationByPipe(PipeInfo pipeInfo, XYZ nodePos, Node.NodeType nodeType)
		{
			if (pipeInfo.IsStandpipe())
			{
				return;
			}
			Element component = pipeInfo.Component;
			if (!(component is Pipe))
			{
				return;
			}
			XYZ xy = this.GetXY(pipeInfo.EndPoint);
			XYZ xy2 = this.GetXY(pipeInfo.StartPoint);
			XYZ xyz = (xy - xy2).Normalize();
			double num = pipeInfo.StartElevation;
			if (xy.IsAlmostEqualTo(nodePos))
			{
				xyz = -xyz;
				num = pipeInfo.EndElevation;
			}
			if (nodeType == Node.NodeType.NT_NULL || nodeType == Node.NodeType.NT_PIPEFITTING)
			{
				xyz = -xyz;
			}
			num = AssistFunc.feetToMM(num) / 1000.0;
			this.DrawElevationLabel(nodePos, xyz, num);
		}

		private void DrawElevationLabel(XYZ ptInsert, XYZ direction, double elevation)
		{
			this.SetProceeding("", 1);
			CurveArray curveArray = DrawingKit.DrawElevationLabel(ptInsert, direction, elevation, this.m_LegendScale, this.m_TextNoteTypeWithLabel, this.m_ViewScale, this.m_Revit, this.View, true, true);
			foreach (object obj in curveArray)
			{
				Curve curve = (Curve)obj;
				this.m_CurveAry.Append(curve);
			}
		}

		private static XYZ GetVector(double angle)
		{
			return new XYZ(Math.Cos(angle), Math.Sin(angle), 0.0);
		}

		private static CurveArray DrawElevationLabel(XYZ ptInsert, XYZ direction, double elevation, double legendScale, TextNoteType textNoteType, int viewScale, ExternalCommandData cmdData, View drawView, bool offset, bool drawBottomLine)
		{
			CurveArray curveArray = new CurveArray();
			CurveArray result;
			try
			{
				double num = AssistFunc.mmToFeet(100.0) * legendScale;
				double num2 = num * 2.0;
				XYZ xyz = ptInsert - direction * (num2 / 2.0);
				XYZ xyz2 = ptInsert + direction * (num2 / 2.0);
				if (offset)
				{
					xyz = ptInsert + direction * num;
					xyz2 = ptInsert + direction * (num + num2);
				}
				XYZ ptStart = xyz + (xyz2 - xyz) / 2.0;
				if (drawBottomLine)
				{
					Line line = DrawingKit.CreateLine(xyz, xyz2);
					if (null != line)
					{
						curveArray.Append(line);
					}
				}
				double angle = Geometry.getAngle(XYZ.Zero, direction);
				double num3 = angle;
				XYZ xyz3;
				if (Geometry.GreaterThan(angle, Math.PI*.5) && Geometry.Lessthan_Or_Equal(angle, 4.71238898038469))
				{
					xyz3 = Geometry.RotateTo(direction, -Math.PI*.5, XYZ.BasisZ);
					num3 = Geometry.formatAngle(num3 - Math.PI);
				}
				else
				{
					xyz3 = Geometry.RotateTo(direction, Math.PI*.5, XYZ.BasisZ);
				}
				string text = elevation.ToString("f3") + "m";
				XYZ xyz4 = xyz + xyz3 * num2 / 2.0;
				XYZ xyz5 = xyz2 + xyz3 * num2 / 2.0;
				XYZ xyz6 = xyz4 + direction * (num2 + (double)text.Length * DrawingKit.TextWidth * (double)viewScale * 0.6);
				Line line2 = DrawingKit.CreateLine(ptStart, xyz4);
				if (null != line2)
				{
					curveArray.Append(line2);
				}
				Line line3 = DrawingKit.CreateLine(ptStart, xyz5);
				if (null != line3)
				{
					curveArray.Append(line3);
				}
				Line line4 = DrawingKit.CreateLine(xyz4, xyz6);
				if (null != line4)
				{
					curveArray.Append(line4);
				}
				XYZ pt = xyz5 + (xyz6 - xyz5) / 2.0;
				XYZ xyz7 = xyz3.Normalize();
				XYZ ptInsert2 = pt.GetXY() + xyz7 * (0.5 * DrawingKit.TextHeight + DrawingKit.TextOffsetLine + UnifiedModified.GetSystemDiagramDrawSize()[1]) * (double)viewScale;
				DrawingKit.DrawText(ptInsert2, DrawingKit.TextWidth * text.GetTextNoteLength(), text, textNoteType, DrawingKit.GetVector(num3), xyz7, cmdData, drawView);
				result = curveArray;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = curveArray;
			}
			return result;
		}

		private static Line CreateLine(XYZ ptStart, XYZ ptEnd)
		{
			Line result;
			try
			{
				result = Line.CreateBound(ptStart, ptEnd);
			}
			catch (Exception ex)
			{
				ex.ToString();
				ex.ToString();
				result = null;
			}
			return result;
		}

		private static Arc CreateArc(ExternalCommandData cmdData, XYZ ptStart, XYZ ptEnd, XYZ ptMid)
		{
			Arc result;
			try
			{
				result = cmdData.Application.ActiveUIDocument.Document.CreatYJKArc(ptStart, ptEnd, ptMid);
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = null;
			}
			return result;
		}

		private Line DrawLineWidthLegend(XYZ ptStart, XYZ ptEnd, Transform trans)
		{
			this.SetProceeding("", 1);
			XYZ ptStart2 = Geometry.TransformPoint(ptStart, trans);
			XYZ ptEnd2 = Geometry.TransformPoint(ptEnd, trans);
			Line result;
			try
			{
				Line line = DrawingKit.CreateLine(ptStart2, ptEnd2);
				result = line;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private XYZ GetXY(XYZ pt)
		{
			return pt.GetXY();
		}

		private Arc DrawArcWidthLegend(XYZ ptStart, XYZ ptEnd, double bugle, Transform trans)
		{
			XYZ xyz = Geometry.TransformPoint(ptStart, trans);
			XYZ xyz2 = Geometry.TransformPoint(ptEnd, trans);
			XYZ ptCenter = null;
			Geometry.GetCenterWithBulge(xyz, xyz2, bugle, ref ptCenter);
			XYZ ptMid = Geometry.CalculatMidPoint(xyz, xyz2, ptCenter, XYZ.BasisZ);
			Arc result;
			try
			{
				Arc arc = DrawingKit.CreateArc(this.m_Revit, xyz, xyz2, ptMid);
				result = arc;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private ElementId DrawTextWidthLegend(XYZ ptCenter, double width, double height, string text, TextNoteType textNoteType)
		{
			TextNote textNote = this.View.CreateTextNote(text, ptCenter);
			return textNote.Id;
		}

		private static ElementId DrawText(XYZ ptInsert, double width, double height, string text, TextNoteType textNoteType, TextAlignFlags alignFlags, ExternalCommandData cmdData, View drawView)
		{
			TextNote textNote = drawView.CreateTextNote(text, ptInsert);
			return textNote.Id;
		}

		private static ElementId DrawText(XYZ ptInsert, double width, string text, TextNoteType textNoteType, XYZ baseDirection, XYZ upDerection, ExternalCommandData cmdData, View drawView)
		{
			TextNote textNote = drawView.CreateTextNote(text, ptInsert, baseDirection);
			return textNote.Id;
		}

		private TextNoteType GetLegendTextNoteType()
		{
			TextNoteType result;
			if ((result = this.m_TextNoteTypeWithLegend) == null)
			{
				result = (this.m_TextNoteTypeWithLegend = this.m_Revit.CreateTextNoteType());
			}
			return result;
		}

		private void DrawNodeLegend(Legend legend, Transform scaleMatrix, XYZ ptInsert, ref List<Line> legendLineList)
		{
			this.SetProceeding("", 1);
			Transform transform = RevitVersionFuncs.CreateTranslation(-this.GetXY(legend.Position));
			Transform transform2 = RevitVersionFuncs.CreateTranslation(this.GetXY(ptInsert));
			Transform transform3 = transform2.Multiply(scaleMatrix);
			transform3 = transform3.Multiply(transform);
			if (this.IsVertical)
			{
				Transform transform4 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, Math.PI*.5, legend.Position);
				transform3 = transform3.Multiply(transform4);
			}
			List<CurveInfo> curveInfoList = legend.CurveInfoList;
			foreach (CurveInfo curveInfo in curveInfoList)
			{
				EdgeInfo curveObj = curveInfo.CurveObj;
				if (Geometry.IsEqual(curveObj.Bulge, 0.0))
				{
					Line line = this.DrawLineWidthLegend(this.GetXY(curveObj.StartPoint), this.GetXY(curveObj.EndPoint), transform3);
					if (null != line)
					{
						this.m_CurveAry.Append(line);
						legendLineList.Add(line);
					}
				}
				else
				{
					Arc arc = this.DrawArcWidthLegend(this.GetXY(curveObj.StartPoint), this.GetXY(curveObj.EndPoint), curveObj.Bulge, transform3);
					if (null != arc)
					{
						this.m_CurveAry.Append(arc);
						IList<XYZ> list = arc.Tessellate();
						for (int i = 0; i < list.Count - 1; i++)
						{
							Line line2 = DrawingKit.CreateLine(list[i], list[i + 1]);
							if (null != line2)
							{
								legendLineList.Add(line2);
							}
						}
					}
				}
			}
			List<TextNoteInfo> textNoteInfoList = legend.TextNoteInfoList;
			TextNoteType legendTextNoteType = this.GetLegendTextNoteType();
			foreach (TextNoteInfo textNoteInfo in textNoteInfoList)
			{
				double width = textNoteInfo.m_BoundingBoxWidth * scaleMatrix.Scale.MMtoFeet() * 0.5;
				double height = textNoteInfo.m_BoundingBoxHeight * scaleMatrix.Scale.MMtoFeet() * 0.5;
				XYZ ptCenter = Geometry.TransformPoint(textNoteInfo.m_Center, transform3);
				this.DrawTextWidthLegend(ptCenter, width, height, textNoteInfo.m_Text, legendTextNoteType);
			}
			List<FillRegion> fillRegionList = legend.FillRegionList;
			ElementId filledRegionTypeId = this.GetFilledRegionTypeId();
			foreach (FillRegion fillRegion in fillRegionList)
			{
				this.DrawFilledRegion(fillRegion, filledRegionTypeId, transform3);
			}
		}

		private void DrawNodeLegend(Legend legend, Transform scaleMatrix, Node node, ref List<Line> legendLineList)
		{
			this.SetProceeding("", 1);
			XYZ position = node.Position;
			Transform transform = RevitVersionFuncs.CreateTranslation(-this.GetXY(legend.Position));
			Transform transform2 = RevitVersionFuncs.CreateTranslation(this.GetXY(position));
			Transform transform3 = transform2.Multiply(scaleMatrix);
			transform3 = transform3.Multiply(transform);
			if (node.IsOnStandPipe && node.IsAccessory)
			{
				Transform transform4 = RevitVersionFuncs.CreateRotationAtPoint(XYZ.BasisZ, 0.78539816339744828, legend.Position);
				transform3 = transform3.Multiply(transform4);
			}
			List<CurveInfo> curveInfoList = legend.CurveInfoList;
			foreach (CurveInfo curveInfo in curveInfoList)
			{
				EdgeInfo curveObj = curveInfo.CurveObj;
				if (Geometry.IsEqual(curveObj.Bulge, 0.0))
				{
					Line line = this.DrawLineWidthLegend(this.GetXY(curveObj.StartPoint), this.GetXY(curveObj.EndPoint), transform3);
					if (null != line)
					{
						node.LegendCurves.Append(line);
					}
				}
				else
				{
					Arc arc = this.DrawArcWidthLegend(this.GetXY(curveObj.StartPoint), this.GetXY(curveObj.EndPoint), curveObj.Bulge, transform3);
					if (!(null == arc))
					{
						this.m_CurveAry.Append(arc);
						IList<XYZ> list = arc.Tessellate();
						for (int i = 0; i < list.Count - 1; i++)
						{
							Line line2 = DrawingKit.CreateLine(list[i], list[i + 1]);
							if (null != line2)
							{
								node.LegendCurves.Append(line2);
							}
						}
					}
				}
			}
			List<TextNoteInfo> textNoteInfoList = legend.TextNoteInfoList;
			TextNoteType legendTextNoteType = this.GetLegendTextNoteType();
			foreach (TextNoteInfo textNoteInfo in textNoteInfoList)
			{
				double width = textNoteInfo.m_BoundingBoxWidth * scaleMatrix.Scale.MMtoFeet() * 0.5;
				double height = textNoteInfo.m_BoundingBoxHeight * scaleMatrix.Scale.MMtoFeet() * 0.5;
				XYZ ptCenter = Geometry.TransformPoint(textNoteInfo.m_Center, transform3);
				ElementId item = this.DrawTextWidthLegend(ptCenter, width, height, textNoteInfo.m_Text, legendTextNoteType);
				node.LegendAttachedIds.Add(item);
			}
			List<FillRegion> fillRegionList = legend.FillRegionList;
			ElementId filledRegionTypeId = this.GetFilledRegionTypeId();
			foreach (FillRegion fillRegion in fillRegionList)
			{
				FilledRegion filledRegion = this.DrawFilledRegion(fillRegion, filledRegionTypeId, transform3);
				node.LegendAttachedIds.Add(filledRegion.Id);
			}
		}

		public IEnumerable<LinkedDocument> LinkedDocuments { get; set; }

		public View View { get; set; }

		public bool IsVertical { get; set; }

		public IProceeding Proceeding { get; set; }

		public void SetProceeding(string message = "", int step = 1)
		{
			if (this.Proceeding == null)
			{
				return;
			}
			this._progress += step;
			this.Proceeding.Report(this._progress, message);
		}

		private Result DrawingFloorLine(PipingSystemInfo pipingSystemInfo)
		{
			this.SetProceeding("绘制楼层线...", 1);
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			View view = this.View;
			List<PipeInfo> pipeInfos = pipingSystemInfo.PipeInfoList();
			IEnumerable<DrawingKit.VerticalPipeInfo> verticalPipeInfos = DrawingKit.VerticalPipeInfo.GetVerticalPipeInfos(pipeInfos);
			verticalPipeInfos.ToList<DrawingKit.VerticalPipeInfo>().ForEach(new Action<DrawingKit.VerticalPipeInfo>(this.DrawFloorLine));
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void DrawFloorLine(DrawingKit.VerticalPipeInfo verticalPipeInfo)
		{
			Transform transform = Transform.Identity;
			transform = transform.ScaleBasis(this.m_LegendScale);
			XYZ xyz = new XYZ(verticalPipeInfo.Positon.X, verticalPipeInfo.Positon.Y, (verticalPipeInfo.TopElevation + verticalPipeInfo.BottomElevation) / 2.0);
			XYZ ptStart = xyz - new XYZ(5.0, 0.0, 0.0);
			XYZ ptEnd = xyz + new XYZ(5.0, 0.0, 0.0);
			this.AddLine(ptStart, ptEnd, transform);
		}

		private static readonly double TextHeight = 2.5.MMtoFeet();

		private static readonly double TextWidth = 2.5.MMtoFeet() * 0.7;

		private static readonly double TextOffsetLine = AssistFunc.mmToFeet(1.0);

		private readonly ExternalCommandData m_Revit;

		private readonly DrawingKit.LegendInfoFileManager m_LegendManager;

		private readonly CurveArray m_CurveAry = new CurveArray();

		private readonly List<KeyValuePair<int, Pipe>> m_PipeLineIndexInCurveAry = new List<KeyValuePair<int, Pipe>>();

		private TextNoteType m_TextNoteTypeWithLegend;

		private readonly TextNoteType m_TextNoteTypeWithLabel;

		private readonly double m_ShaftSideAngle;

		private readonly double m_LegendScale;

		private readonly int m_ViewScale;

		private int _progress;

		public enum LabelType
		{
			LT_NearInside,
			LT_NearExternal,
			LT_SelectPoint
		}

		public class LegendInfoFileManager
		{
			public LegendInfoFileManager()
			{
				this.ReadMappingFile();
				this.ReadLegendData();
			}

			public LegendGroup GetLegendGroupByFamilyName(string familyName)
			{
				if (string.IsNullOrEmpty(familyName))
				{
					return null;
				}
				string legendName = this.GetLegendName(familyName);
				if (string.IsNullOrEmpty(legendName))
				{
					return null;
				}
				return this.m_LegendGroupList.Find((LegendGroup s) => string.Compare(s.TheName, legendName, StringComparison.Ordinal) == 0);
			}

			public LegendGroup GetLegendGroupByName(string legendName)
			{
				if (string.IsNullOrEmpty(legendName))
				{
					return null;
				}
				return this.m_LegendGroupList.Find((LegendGroup s) => string.Compare(s.TheName, legendName, StringComparison.Ordinal) == 0);
			}

			private void ReadLegendData()
			{
				string fileName = LegendInfoFilePathKit.LegendDataFilePath();
				this.m_LegendGroupList = XMLKit.ReadLegendXMLFile(fileName);
			}

			private void ReadMappingFile()
			{
				string fileName = LegendInfoFilePathKit.LegendMappingInfoFilePath();
				this.m_MappingDataList = XMLKit.ReadMappingFile(fileName);
			}

			private string GetLegendName(string familyName)
			{
				string text = "";
				if (this._dictFaNameToLegendName != null && this._dictFaNameToLegendName.TryGetValue(familyName, out text))
				{
					return text;
				}
				MappingData mappingData = this.m_MappingDataList.Find((MappingData s) => string.Compare(s.FamilyName, familyName, StringComparison.Ordinal) == 0);
				if (mappingData != null)
				{
					text = mappingData.LegendName;
				}
				else
				{
                    //var <>f__AnonymousType = (from s in this.m_MappingDataList
                    //where s.FamilyName.Contains(familyName) || familyName.Contains(s.FamilyName)
                    //select new
                    //{
                    //    Family = s.FamilyName,
                    //    Legend = s.LegendName
                    //}).FirstOrDefault();
                    //if (<>f__AnonymousType == null)
                    //{
                    //    return null;
                    //}
                    //text = <>f__AnonymousType.Legend;
				}
				if (this._dictFaNameToLegendName == null)
				{
					this._dictFaNameToLegendName = new Dictionary<string, string>();
				}
				this._dictFaNameToLegendName.Add(familyName, text);
				return text;
			}

			public static string LCS(string s1, string s2)
			{
				if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
				{
					return null;
				}
				if (string.Compare(s1, s2, StringComparison.Ordinal) == 0)
				{
					return s1;
				}
				int[,] array = new int[s1.Length, s2.Length];
				int num = 0;
				int num2 = 0;
				for (int i = 0; i < s1.Length; i++)
				{
					for (int j = 0; j < s2.Length; j++)
					{
						int num3 = (i - 1 >= 0 && j - 1 >= 0) ? array[i - 1, j - 1] : 0;
						array[i, j] = ((s1[i] == s2[j]) ? (1 + num3) : 0);
						if (array[i, j] > num2)
						{
							num2 = array[i, j];
							num = i;
						}
					}
				}
				return s1.Substring(num - num2 + 1, num2);
			}

			public static int LCSLength(string s1, string s2)
			{
				if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
				{
					return 0;
				}
				if (s1.Contains(s2))
				{
					return s2.Length;
				}
				int[,] array = new int[s1.Length, s2.Length];
				int num = 0;
				for (int i = 0; i < s1.Length; i++)
				{
					for (int j = 0; j < s2.Length; j++)
					{
						int num2 = (i - 1 >= 0 && j - 1 >= 0) ? array[i - 1, j - 1] : 0;
						array[i, j] = ((s1[i] == s2[j]) ? (1 + num2) : 0);
						if (array[i, j] > num)
						{
							num = array[i, j];
						}
					}
				}
				return num;
			}

			private List<LegendGroup> m_LegendGroupList = new List<LegendGroup>();

			private List<MappingData> m_MappingDataList = new List<MappingData>();

			private Dictionary<string, string> _dictFaNameToLegendName;
		}

		private class VerticalPipeInfo
		{
			public double BottomElevation { get; set; }

			public double TopElevation { get; set; }

			public XYZ Positon { get; set; }

			public XYZ TopPosition
			{
				get
				{
					return new XYZ(this.Positon.X, this.Positon.Y, this.TopElevation);
				}
			}

			public XYZ BottomPosition
			{
				get
				{
					return new XYZ(this.Positon.X, this.Positon.Y, this.BottomElevation);
				}
			}

			public string ToLogString()
			{
				return string.Format("Pos = [{0},{1}] \t,Top={2}\t,Bot={3}", new object[]
				{
					this.Positon.X,
					this.Positon.Y,
					this.TopElevation,
					this.BottomElevation
				});
			}

			public static DrawingKit.VerticalPipeInfo From(PipeInfo pi)
			{
				return DrawingKit.VerticalPipeInfo.IsVertical(pi) ? new DrawingKit.VerticalPipeInfo
				{
					Positon = pi.StartPoint,
					TopElevation = Math.Max(pi.StartPoint.Z, pi.EndPoint.Z),
					BottomElevation = Math.Min(pi.StartPoint.Z, pi.EndPoint.Z)
				} : null;
			}

			public bool Merge(PipeInfo pi)
			{
				return this.Merge(DrawingKit.VerticalPipeInfo.From(pi));
			}

			public bool Merge(DrawingKit.VerticalPipeInfo vp)
			{
				if (vp == null)
				{
					return false;
				}
				if (!DrawingKit.VerticalPipeInfo.IsXYEqual(vp.Positon, this.Positon))
				{
					return false;
				}
				this.TopElevation = Math.Max(vp.TopElevation, this.TopElevation);
				this.BottomElevation = Math.Min(vp.BottomElevation, this.BottomElevation);
				return true;
			}

			private static bool IsVertical(PipeInfo pipe)
			{
				return pipe.IsStandpipe();
			}

			private static bool IsXYEqual(XYZ p1, XYZ p2)
			{
				return Geometry.IsEqual(p1.X, p2.X, 0.0001) && Geometry.IsEqual(p1.Y, p2.Y, 0.0001) && !Geometry.IsEqual(p1.Z, p2.Z, 0.0001);
			}

			public static bool IsXYEqual(PipeInfo pi1, PipeInfo pi2)
			{
				return DrawingKit.VerticalPipeInfo.IsXYEqual(pi1.StartPoint, pi2.StartPoint);
			}

			public static bool IsXYEqual(DrawingKit.VerticalPipeInfo pi1, DrawingKit.VerticalPipeInfo pi2)
			{
				return DrawingKit.VerticalPipeInfo.IsXYEqual(pi1.Positon, pi2.Positon);
			}

			public static IEnumerable<DrawingKit.VerticalPipeInfo> GetVerticalPipeInfos_old(IEnumerable<PipeInfo> pipeInfos)
			{
				List<DrawingKit.VerticalPipeInfo> res = new List<DrawingKit.VerticalPipeInfo>();
				if (pipeInfos == null || !pipeInfos.Any<PipeInfo>())
				{
					return res;
				}
				IEnumerable<DrawingKit.VerticalPipeInfo> source = pipeInfos.Where(new Func<PipeInfo, bool>(DrawingKit.VerticalPipeInfo.IsVertical)).Select(new Func<PipeInfo, DrawingKit.VerticalPipeInfo>(DrawingKit.VerticalPipeInfo.From));
				if (!source.Any<DrawingKit.VerticalPipeInfo>())
				{
					return res;
				}
				source.ToList<DrawingKit.VerticalPipeInfo>().ForEach(delegate(DrawingKit.VerticalPipeInfo vp)
				{
					DrawingKit.VerticalPipeInfo.AddToList(vp, res);
				});
				return res;
			}

			public static IEnumerable<DrawingKit.VerticalPipeInfo> GetVerticalPipeInfos(IEnumerable<PipeInfo> pipeInfos)
			{
				List<DrawingKit.VerticalPipeInfo> res = new List<DrawingKit.VerticalPipeInfo>();
				if (pipeInfos == null || !pipeInfos.Any<PipeInfo>())
				{
					return res;
				}
				IEnumerable<DrawingKit.VerticalPipeInfo> source = pipeInfos.Where(new Func<PipeInfo, bool>(DrawingKit.VerticalPipeInfo.IsVertical)).Select(new Func<PipeInfo, DrawingKit.VerticalPipeInfo>(DrawingKit.VerticalPipeInfo.From));
				if (!source.Any<DrawingKit.VerticalPipeInfo>())
				{
					return res;
				}
				source.ToList<DrawingKit.VerticalPipeInfo>().ForEach(delegate(DrawingKit.VerticalPipeInfo vp)
				{
					DrawingKit.VerticalPipeInfo.AddToList(vp, res);
				});
				return res;
			}

			public static void AddToList(DrawingKit.VerticalPipeInfo vp, List<DrawingKit.VerticalPipeInfo> vps)
			{
				DrawingKit.VerticalPipeInfo verticalPipeInfo = vps.Find((DrawingKit.VerticalPipeInfo p) => DrawingKit.VerticalPipeInfo.IsXYEqual(p, vp));
				if (verticalPipeInfo == null)
				{
					vps.Add(vp);
					return;
				}
				verticalPipeInfo.Merge(vp);
			}
		}
	}
}
